package org.sgx.gwtjsutiljsfcgenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class MainGenerator {
	int upto =3;
	String[] names = { "V", "O", "B", "N", "S" };
//	private File parentFolder;
	private SingleGenerator gen;

	public void generate(File parentFolder) throws BadSignature, IOException {
//		this.parentFolder = parentFolder; 
		List<String> result = new LinkedList<String>(); 
		printAllLetterSequences(result, "", upto);
		gen = new SingleGenerator();
		
		for (Iterator<String> iterator = result.iterator(); iterator.hasNext();) {			
			String s = iterator.next();
			if(s.length()<2)
				continue;
//			System.out.println(s);
			StringWriter w = new StringWriter();
			try {
				gen.generate(w, s);			
			} catch (BadSignature e) {
				continue; 
			}
			
			FileWriter fw = new FileWriter(parentFolder.getAbsolutePath()+File.separator+s+".java");
			fw.write(w.toString()); 
			fw.close(); 
		}
	}

	/** 
	 * recursive method for generating all letters combination
	 * @param result
	 * @param prefix
	 * @param length
	 */
	void printAllLetterSequences(List<String> result, String prefix, int length) {
		result.add(prefix); 
	    if (prefix.length() < length)
	    	for (int i = 0; i < names.length; i++) {
	    	    printAllLetterSequences(result, prefix + names[i], length);	
			}
	        
	}
//	List<String> generateAllNames(int size) {
//		List<String> l = new LinkedList<String>();
//		for (int i = 0; i < size; i++) {
//			
//		}
//		
//		String[][] comb = new String[][];
//		for (int i = 0; i < names.length; i++) {
//			
//		}
//		return l;
//	}
	
	public static void main(String[] args) {
//		test1();
		try {
			test2();
		} catch (BadSignature e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void test2() throws BadSignature, IOException {
		String folderPath = "/home/sebastian/workspace 3.7rc2/gwtjsutil/src/org/sgx/jsutil/client/jsfc";
		File dir = new File(folderPath);
		dir.mkdirs();
		new MainGenerator().generate(dir); 
	}

	private static void test1() {
		List<String> result = new LinkedList<String>(); 
		new MainGenerator().printAllLetterSequences(result, "", 7);
		for (Iterator iterator = result.iterator(); iterator.hasNext();) {
			String s = (String) iterator.next();
			System.out.println(s);
			
		}
	}
}
